package com.detooa.detooa.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.detooa.common.core.controller.BaseController;
import com.detooa.common.core.domain.AjaxResult;
import com.detooa.detooa.utils.DeUtils;
import com.detooa.detooa.utils.bean.de.response.BaseDeVo;
import com.detooa.detooa.utils.bean.de.response.ChildrenVo;
import com.detooa.detooa.utils.bean.de.response.DeLinkVo;
import com.detooa.detooa.utils.bean.de.response.DeTreeVo;

/***
 * 对接飞致云DataEase控制类
 */
@Controller
@RequestMapping("/detooa/de")
public class DeController extends BaseController {

    private String prefix = "detooa/dataease";

    @RequiresPermissions("detooa:de:view")
    @GetMapping()
    public String tree()
    {
        return prefix + "/tree";
    }

    @RequiresPermissions("detooa:de:view")
    @GetMapping("/treeData")
    @ResponseBody
    public List<ChildrenVo> treeData(String cacheName, ModelMap mmap)
    {
        DeTreeVo tree = new DeTreeVo();
        try {
            tree = DeUtils.toDeTreeVo(DeUtils.tree());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tree.getData();
    }

    /**
     * 压缩链接 
     * @param resourceId
     * @param mmap
     * @return
     */
    @RequiresPermissions("detooa:de:view")
    @GetMapping("/shortUrl/{resourceId}")
    @ResponseBody
    public AjaxResult shortUrl(@PathVariable("resourceId") String resourceId, ModelMap mmap)
    {
        try {
            BaseDeVo baseDeVo = DeUtils.shortUrl(resourceId);
            // 判断本地是否有记录
            return success(baseDeVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return error();
    }

    /***
     * 当前链接信息
     * 
     * @param resourceId
     * @param mmap
     * @return
     */
    @RequiresPermissions("detooa:de:view")
    @GetMapping("/currentGenerate/{resourceId}")
    @ResponseBody
    public AjaxResult currentGenerate(@PathVariable("resourceId") String resourceId, ModelMap mmap)
    {
        try {
            DeLinkVo deLinkVo = DeUtils.currentGenerate(resourceId);
            // 判断本地是否有记录
            return success(deLinkVo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return error();
    }

    /**
     * 选择部门树
     * 
     * @param deptId 部门ID
     */
    @RequiresPermissions("detooa:de:list")
    @GetMapping("/selectResourceTree")
    public String selectResourceTree(String resourceId, String resourceName, ModelMap mmap)
    {
        mmap.addAttribute("resourceId", resourceId);
        mmap.addAttribute("resourceName", resourceName);
        return prefix + "/resourceTree";
    }
}
